/*
 * @lc app=leetcode.cn id=819 lang=cpp
 *
 * [819] 最常见的单词
 */

// @lc code=start
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
    bool check(char c)
    {
        return (c >= 'a' && c <= 'z');
    }
    string mostCommonWord(string paragraph, vector<string>& banned) {
        map<string, int> words;
        for(auto& c : paragraph)
        {
            if(c >= 'A' && c <= 'Z')
            {
                c = c - 'A' + 'a';
            }
        }
        for(int i = 0, j = i + 1; i < paragraph.size(); i = j)
        {
            if(check(paragraph[i]))
            {
                j = i;
                while(++j < paragraph.size() && check(paragraph[j]));
                
                words[paragraph.substr(i, j - i)]++;
            }
            else
            {
                j++;
            }
        }

        set<string> b;
        for(auto& s : banned)
        {
            b.insert(s);
        }

        int count = 0;
        string ans;
        for(auto& w : words)
        {
            if(b.find(w.first) != b.end()) continue;
            if(w.second > count) count = w.second, ans = w.first;
        }
        return ans;
    }
};
// @lc code=end

